package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.enumeration.MemberStatus;
import cn.com.connext.scrm.domain.master.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemberRepository extends JpaRepository<Member, String> {
    ///根据unionId查询某个会员
    Member findOneByUnionId(String unionId);

    ///根据昵称模糊查询所有对应的unionId
    @Query(value = "select m.unionId from Member m where m.nickName like %?1% order by m.createTime desc")
    List<String> findAllUnionIdByNickNameLike(String nickName);

    ///查询所有被删除的会员unionId
    @Query(value = "select m.unionId from Member m where m.status = 'DELETED'")
    List<String> findUnionIdWithoutUse();

    ///根据状态查询所有会员
    List<Member> findAllByStatus(MemberStatus memberStatus);
    ///根据状态查询所有会员
    @Query(value = "select m.unionId from Member m where m.mobile in (?1) and m.status = ?2")
    List<String> findUnionIdByMobileAndStatus(List<String> mobileList,MemberStatus memberStatus);

    //根据membershipId查询会员信息
    Member findByMembershipId(String membershipId);

    //根据会员unionId查询会员信息
    @Query("select m from Member m where m.unionId = ?1 ")
    Member findByUnionId(String unionId);

    @Query(value = "select m from Member m  where m.unionId in ?1 ")
    List<Member> findByUserIds(List<String> list);

    //根据会员unionId/mobile查询会员信息
    @Query(value = "select * from member m where m.union_id = ?1 or m.mobile = ?2 limit 1", nativeQuery = true)
    Member findByUnionIdAndMobile(String unionId, String mobile);

    @Query(value = "(SELECT * FROM member WHERE id = :membershipId)", nativeQuery = true)
    Member getMemberById (@Param("membershipId") String membershipId);

    @Query(value = "(SELECT * FROM member WHERE membership_id = :membershipId)", nativeQuery = true)
    Member getMembershipId (@Param("membershipId") String membershipId);
}
